Linked List

PDF versionPDF version

#include<stdio.h>
#include<alloc.h>
#include<conio.h>

int i=0;
int a;

struct node
{
 int data;
 struct node *link;
}*start,*temp,*x,*temp1;

void main()
{
 int ch;
 char ans;

 void insertbeg();
 void insertmid();
 void insertend();

 void delbeg();
 void delmid();
 void delend();
 void delall();

 void view();

 clrscr();
 do
 {

 printf("1:Insert");
 printf("\n2:Delete ");
 printf("\n3:View");

 printf("\nEnter your choice =>");
 scanf("%d",&ch);

 switch(ch)
 {
  case 1:{
  printf("\n1:Insert at Beginning");
  printf("\n2:Insert in Middle");
  printf("\n3:Insert at End");

  printf("\nEnter your choice =>");
  fflush(stdin);
  scanf("%d",&ch);
  switch(ch)
  {
   case 1: {   insertbeg();
     break;
    }

   case 2: {       insertmid();
     break;
    }
   case 3: {       insertend();
     break;
    }
  }
   break;
  }
  case 2:{
  printf("\n1:Delete First Element");
  printf("\n2:Delete from Middle");
  printf("\n3:Delete last Element");
  printf("\n4:Delete Entire List");

  printf("\nEnter your choice =>");
  fflush(stdin);
  scanf("%d",&ch);
  switch(ch)
  {
   case 1: {   delbeg();
     break;
    }

   case 2: {       delmid();
     break;
    }
   case 3: {       delend();
     break;
    }
   case 4: {       delall();
     break;
    }
  }
     break;
   }
  case 3:{
  view();
  break;
  }
  default:{
  printf("\nYou have entered a wrong choice ");
   }
 }

 printf("\n\nDo you want to continue =>(y/n)");
 fflush(stdin);
 scanf("%c",&ans);

}while(ans=='y' || ans=='Y');

getch();

}

void insertbeg()
{
  if(i==0)
  {
  temp=start;
  x=(struct node*)malloc((sizeof(struct node*)));
  printf("Enter the data =>");
  scanf("%d",&x->data);

  x->link=NULL;
  start=x;
  temp=start;
  i=i+1;
  }
  else
  {
   temp=start;
  x=(struct node*)malloc((sizeof(struct node*)));
  printf("Enter the data =>");
  scanf("%d",&x->data);

  x->link=start;
  start=x;
  temp=start;
  i=i+1;
  }

  printf("\n Node Created ");
}

void insertmid()
{
  temp=start;

  printf("Enter the data after which u want the node =>");
  scanf("%d",&a);
  while(temp->data!=a)
 {
  temp=temp->link;
 }
  x=(struct node*)malloc((sizeof(struct node*)));
  printf("Enter the data =>");
  scanf("%d",&x->data);

  x->link=temp->link;
  temp->link=x;
  temp=start;
  i=i+1;
  printf("\nNode Created ");

}

void insertend()
{
  temp=start;
  x=(struct node*)malloc((sizeof(struct node*)));
  printf("Enter the data =>");
  scanf("%d",&x->data);
  while(temp->link!=NULL)
  {
   temp=temp->link;
  }
  temp->link=x;
  x->link=NULL;
  printf("\n Node Created ");
  i=i+1;
}

void delbeg()
{
 temp=start;
 start=start->link;
 free(temp);
 temp=start;
 i=i-1;
 printf("\nFirst Node Deleted....!!");
}

void delmid()
{
  temp=start;

  printf("Enter the data to be deleted =>");
  scanf("%d",&a);

  while(temp->data!=a)
  {
   temp1=temp;
   temp=temp->link;
  }

  temp1->link=temp->link;
  free(temp);
  i=i-1;
  printf("\nElement Deleted...!!");

}

void delend()
{
 temp=start;

 while(temp->link!=NULL)
 {
  temp1=temp;
  temp=temp->link;
 }
 temp1->link=NULL;
 free(temp);
 i=i-1;
 printf("\nLast Element Deleted...!!");

}

void delall()
{
 temp=start;

 while(start!=NULL)
 {
  temp1=temp;
  temp=temp->link;
  free(temp1);
  i=i-1;
  start=temp;
 }
 printf("\nAll Elements Deleted....List Empty..");

}

void view()
{
 if(i==0)
 {
  printf("Linked list Empty...!! ");
 }
 else
 {
 temp=start;
 while(temp->link!=NULL)
 {
  printf(" %d -> ",temp->data);

  temp=temp->link;
 }
 printf("%d -> NULL",temp->data);
 }
}